package lib.self.adapter;

import androidx.annotation.IdRes;
import android.util.SparseArray;
import android.view.View;

import lib.self.adapter.interfaces.IViewHolder;

/**
 * description: ViewPager ListView 通用 ViewHolder
 * author: Steven Xiao
 * date: 2017/6/15
 */
public abstract class ViewHolderEx implements IViewHolder {

    private View mConvertView;

    private SparseArray<View> mMap;

    public ViewHolderEx(View convertView) {
        mConvertView = convertView;
        mMap = new SparseArray<>();
    }

    @Override
    public View getConvertView() {
        return mConvertView;
    }

    @Override
    public <T extends View> T findViewById(@IdRes int id) {
        return (T) mConvertView.findViewById(id);
    }

    @Override
    public <T extends View> T getView(@IdRes int id) {
        View view = mMap.get(id);
        if (view == null) {
            view = findViewById(id);
            mMap.put(id, view);
        }
        return (T) view;
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        mMap.clear();
        mMap = null;
    }
}
